package com.opmsys.controller;

import com.opmsys.pojo.dto.WellPageDTO;
import com.opmsys.pojo.entity.Well;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.PumpingService;
import com.opmsys.service.WellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名称WellController
 * 描述
 *
 * @version 1.0
 * @author:冉启呈2021015445
 * @datatime:2024-06-17 16:28
 */
@RestController
@RequestMapping("/well")
public class WellController {
    @Autowired
    public WellService wellService;

    @Autowired
    public PumpingService pumpingService;


    /**
     * 添加油井
     * @param well
     * @return
     */
    @PostMapping
    public Result addWell(Well well) {
        List<Well> isRepeat=wellService.ifname(well.wellName);
        if(isRepeat.size()!= 0)
            return Result.error("名字重复");
        else {
            wellService.addWell(well);
            return Result.successWithMsg("新增成功");
        }
    }


    /**
     * 批量删除油井
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deletePumpingBatch(@RequestParam(required = false) List<Integer> ids) {
        for (Integer id : ids) {
            wellService.deleteWells(id);
            pumpingService.setWellNull(id);
        }
        return Result.successWithMsg("删除成功");
    }


    /**
     * 更新油井
     * @param well
     * @return
     */
    @PutMapping
    public Result updateWell(Well well) {
        List<Well> isRepeat=wellService.ifname(well.wellName);
        if(isRepeat.size()!= 0)
            return Result.error("名字重复");
        else{
            wellService.updateWell(well);
            return Result.successWithMsg("更新成功");
        }
    }


    /**
     * 模糊查询井名与精准查询井类型
     * @param wellPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> searchWells(WellPageDTO wellPageDTO) {
        PageResult result=new PageResult();
        List<Well> wells=wellService.searchWells(wellPageDTO.getCharacteristic(),wellPageDTO.getType());
        int num=wells.size();
        result.setTotal(num);
        List<Well> wells2=wellService.pageWells(wellPageDTO);
        result.setRecords(wells2);
        return Result.success(result);
    }
}
